#include<mysql.h>
#include<stdio.h>
#include<unistd.h>
#include<functions.h>
extern MYSQL *mysql;
int sys_search(char *query)
{
	char sc;
	if(mysql_query(mysql, query))
	{
		printf("查询错误:%s \n", mysql_error(mysql));
		sleep(5);
		return 0;
	}
	MYSQL_RES *result = mysql_store_result(mysql);
	if(result == NULL)
	{
		fprintf(stderr, "%s\n", mysql_error(mysql));
		sleep(5);
		return 0;
	}
	int num_fields = mysql_num_fields(result);
	MYSQL_ROW row;
	while((row = mysql_fetch_row(result)))
	{
		for(int i = 0; i < num_fields; i++)
		{
			/*if(i == 1)
			for(int j = printf(" %s", row[i] ? row[i] : "NULL"); j < 15; j ++)
				putchar(' ');
			else*/
			for(int j = printf(" %s", row[i] ? row[i] : "NULL"); j < 12; j ++)
				putchar(' ');
		}
		printf("\n");
	}
	printf("回车退出!\n");
	scanf("%c", &sc);
	scanf("%c", &sc);
	mysql_free_result(result);
	return 1;
}
int sys_insert(char *query)
{
	char sc;
	if(!sys_error(query))
		return 0;
	printf("输入回车结束\n");
	scanf("%c", &sc);	scanf("%c", &sc);
	return 1;
}
int sys_delete(char *query)
{
	char sc;
	int ID;
	char mysql_delete[50];
	scanf("%d", &ID);
	if(ID == 0)	return 0;
	sprintf(mysql_delete, "delete from %s where ID='%d'", query, ID);
	if(!sys_error(mysql_delete))
		return 0;
	printf("输入回车结束\n");
	scanf("%c", &sc); scanf("%c", &sc);
	return 1;
}
int sys_error(char *query)
{
	if(mysql_query(mysql, query))
	{
		fprintf(stderr, "操作失败:%s (输入回车继续)\n", mysql_error(mysql));
		char sc;scanf("%c",&sc);scanf("%c",&sc);
		return 0;
	}
	else
	{
		printf("操作成功！\n");
	}
	return 1;
}
